/***************************************************************************
创建者：　华磊
 开始时间：               : 2020.11.09
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   程序寸动视窗测试用代理类
*                                                                         *
 ***************************************************************************/
#include "jogdelegate.h"
#include "hmicommondefine.h"
#include "instructionEnum.h"
#include <QDebug>

JogDelegate::JogDelegate(MoveOperation *moveOperationIn, JogView *jogViewIn,
                         GlobalData *globalDataIn, QObject *parent):
        QObject(parent)
{
    globalData = globalDataIn;
    moveOperation=moveOperationIn;
    jogView=jogViewIn;

    axisNumber = EM_AXIS_END;       //未初始化
    direction = EM_DIRECTION_END;

    initConnect();
}

void JogDelegate::initConnect()
{
    connect(jogView, SIGNAL(pushButton_L1_pressedSignal()), this, SLOT(pushButton_L1_pressedSlot()));
    connect(jogView, SIGNAL(pushButton_L2_pressedSignal()), this, SLOT(pushButton_L2_pressedSlot()));
    connect(jogView, SIGNAL(pushButton_L3_pressedSignal()), this, SLOT(pushButton_L3_pressedSlot()));
    connect(jogView, SIGNAL(pushButton_L4_pressedSignal()), this, SLOT(pushButton_L4_pressedSlot()));
    connect(jogView, SIGNAL(pushButton_L5_pressedSignal()), this, SLOT(pushButton_L5_pressedSlot()));
    connect(jogView, SIGNAL(pushButton_L6_pressedSignal()), this, SLOT(pushButton_L6_pressedSlot()));
    connect(jogView, SIGNAL(pushButton_L7_pressedSignal()), this, SLOT(pushButton_L7_pressedSlot()));

    connect(jogView, SIGNAL(pushButton_L1_releasedSignal()), this, SLOT(pushButton_L1_releasedSlot()));
    connect(jogView, SIGNAL(pushButton_L2_releasedSignal()), this, SLOT(pushButton_L2_releasedSlot()));
    connect(jogView, SIGNAL(pushButton_L3_releasedSignal()), this, SLOT(pushButton_L3_releasedSlot()));
    connect(jogView, SIGNAL(pushButton_L4_releasedSignal()), this, SLOT(pushButton_L4_releasedSlot()));
    connect(jogView, SIGNAL(pushButton_L5_releasedSignal()), this, SLOT(pushButton_L5_releasedSlot()));
    connect(jogView, SIGNAL(pushButton_L6_releasedSignal()), this, SLOT(pushButton_L6_releasedSlot()));
    connect(jogView, SIGNAL(pushButton_L7_releasedSignal()), this, SLOT(pushButton_L7_releasedSlot()));

    connect(jogView, SIGNAL(pushButton_R1_pressedSignal()), this, SLOT(pushButton_R1_pressedSlot()));
    connect(jogView, SIGNAL(pushButton_R2_pressedSignal()), this, SLOT(pushButton_R2_pressedSlot()));
    connect(jogView, SIGNAL(pushButton_R3_pressedSignal()), this, SLOT(pushButton_R3_pressedSlot()));
    connect(jogView, SIGNAL(pushButton_R4_pressedSignal()), this, SLOT(pushButton_R4_pressedSlot()));
    connect(jogView, SIGNAL(pushButton_R5_pressedSignal()), this, SLOT(pushButton_R5_pressedSlot()));
    connect(jogView, SIGNAL(pushButton_R6_pressedSignal()), this, SLOT(pushButton_R6_pressedSlot()));
    connect(jogView, SIGNAL(pushButton_R7_pressedSignal()), this, SLOT(pushButton_R7_pressedSlot()));

    connect(jogView, SIGNAL(pushButton_R1_releasedSignal()), this, SLOT(pushButton_R1_releasedSlot()));
    connect(jogView, SIGNAL(pushButton_R2_releasedSignal()), this, SLOT(pushButton_R2_releasedSlot()));
    connect(jogView, SIGNAL(pushButton_R3_releasedSignal()), this, SLOT(pushButton_R3_releasedSlot()));
    connect(jogView, SIGNAL(pushButton_R4_releasedSignal()), this, SLOT(pushButton_R4_releasedSlot()));
    connect(jogView, SIGNAL(pushButton_R5_releasedSignal()), this, SLOT(pushButton_R5_releasedSlot()));
    connect(jogView, SIGNAL(pushButton_R6_releasedSignal()), this, SLOT(pushButton_R6_releasedSlot()));
    connect(jogView, SIGNAL(pushButton_R7_releasedSignal()), this, SLOT(pushButton_R7_releasedSlot()));
}

void JogDelegate::updateAxisValueShow()
{

    if(globalData)
    {
        //界面data刷新
//        if(globalData->tryLock() )
//        {
        PointPro currentPositionJointOut;
        globalData->getCurrentArmPosition_joint(currentPositionJointOut);
        PointPro currentPositionDecartOut;
        globalData->getCurrentArmPosition_decart(currentPositionDecartOut);

            if(globalData->globalCoordinate == EM_JOG_COORDINATE_JOINT)//关节
            {
                jogView->updateAxisPositionShow(true,globalData->robotAxisCount,globalData->jogPageIndex,
                                                globalData->onePageAxisCount,
                                                currentPositionJointOut.positionValue);


            }
            else
            {
                jogView->updateAxisPositionShow(false,globalData->robotAxisCount,0,
                                                globalData->onePageAxisCount,
                                                currentPositionDecartOut.positionValue);
            }


//            globalData->unlock();


//        }
    }


}

void JogDelegate::updateAxisPropertyShow()
{


    static bool isNeedShowAxisProperty=true;
    if(globalData)
    {
        std::vector<std::vector<float> > axisLimitOut;
        std::vector<int> axisUnitOut;
        globalData->getAxisUnitAndLimit(axisLimitOut,axisUnitOut);

//        if(globalData->tryLock() )
//        {
            static int tmpCoordinateJog;
            static     int beforeRobotID=0;
            static int tmpAxisPageId=0;
            if(isNeedShowAxisProperty) //需要初始化 只执行一次
            {
                if(0==axisLimitOut.size()|| 0==axisUnitOut.size())
                {
//                    globalData->unlock();
                    return ;
                }
                qDebug()<<"isNeedShowAxisProperty true";
                if(globalData->gsoapCtrlState == EM_UPDATE_STATUS_LV2)      //LV1已经完成
                {
                    tmpCoordinateJog=globalData->globalCoordinate;

                    if(globalData->globalCoordinate == EM_JOG_COORDINATE_JOINT)
                    {

                        jogView->changePageAxisStyleUpdate(true,globalData->robotAxisCount,globalData->jogPageIndex,
                                                           globalData->onePageAxisCount,
                                                           axisUnitOut,
                                                           axisLimitOut);
                        emit changeJogAxisName_signal(true,globalData->robotAxisCount,globalData->jogPageIndex,
                                                      globalData->onePageAxisCount);

                    }
                    else//显示笛卡尔
                    {
                        jogView->changePageAxisStyleUpdate(false,globalData->robotAxisCount,0,
                                                           globalData->onePageAxisCount,
                                                           axisUnitOut,
                                                           axisLimitOut);
                        emit changeJogAxisName_signal(false,globalData->robotAxisCount,0,
                                                      globalData->onePageAxisCount);
                    }
                    isNeedShowAxisProperty=false;

                }
//                globalData->unlock();

                return;
            }



            //判断当前是轴页面
            if(tmpAxisPageId!=globalData->jogPageIndex )
            {
                //qDebug()<<"判断当前是外部轴还是本体轴";
                isNeedShowAxisProperty = true;//重启初始化
                tmpAxisPageId = globalData->jogPageIndex;
//                globalData->unlock();
                qDebug()<<"轴页面changed";
                return;
            }

            //判断机器人有无被切换 有就刷新
            if(globalData->currentRobotID != beforeRobotID)
            {
                //qDebug()<<"判断机器人有无被切换";
                isNeedShowAxisProperty = true;//重启初始化
                beforeRobotID = globalData->currentRobotID;

//                globalData->unlock();
                qDebug()<<"机器人changed";
                return;
            }

            //判断坐标系是否变换
            if(tmpCoordinateJog!=globalData->globalCoordinate)
            {
                isNeedShowAxisProperty = true;//重启初始化
//                globalData->unlock();
                qDebug()<<"globalCoordinate　changed";
                return;
            }



//            globalData->unlock();

//        }
    }



}

JogDelegate::~JogDelegate()
{

}

void JogDelegate::pushButton_L1_pressedSlot()
{
    //qDebug("pushButton_L1_pressed");
    axisNumber = EM_AXIS_1;
    direction = EM_DIRECTION_LEFT;
    emit setRightViewCurrentAxis(EM_AXIS_1);

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_L2_pressedSlot()
{
    //qDebug("pushButton_L2_pressed");
    axisNumber = EM_AXIS_2;
    direction = EM_DIRECTION_LEFT;

    emit setRightViewCurrentAxis(EM_AXIS_2);

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_L3_pressedSlot()
{
    //qDebug("pushButton_L3_pressed");
    axisNumber = EM_AXIS_3;
    direction = EM_DIRECTION_LEFT;

    emit setRightViewCurrentAxis(EM_AXIS_3);

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_L4_pressedSlot()
{
    //qDebug("pushButton_L4_pressed");
    axisNumber = EM_AXIS_4;
    direction = EM_DIRECTION_LEFT;

    emit setRightViewCurrentAxis(EM_AXIS_4);

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_L5_pressedSlot()
{
    //qDebug("pushButton_L5_pressed");
    axisNumber = EM_AXIS_5;
    direction = EM_DIRECTION_LEFT;
    emit setRightViewCurrentAxis(EM_AXIS_5);

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_L6_pressedSlot()
{
    //qDebug("pushButton_L6_pressed");
    axisNumber = EM_AXIS_6;
    direction = EM_DIRECTION_LEFT;
    emit setRightViewCurrentAxis(EM_AXIS_6);

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_L7_pressedSlot()
{
//    //qDebug("pushButton_L6_pressed");
//    axisNumber = EM_AXIS_7;
//    direction = EM_DIRECTION_LEFT;
//    emit setRightViewCurrentAxis(EM_AXIS_7);

//    //jogTimer->start(JOG_TIME);
//    //新调用方式
//    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_L1_releasedSlot()
{
    //qDebug("pushButton_L1_released");

    //新调用方式
    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_L2_releasedSlot()
{
    //qDebug("pushButton_L2_released");

    //新调用方式
    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_L3_releasedSlot()
{
    //qDebug("pushButton_L3_released");

    //新调用方式
    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_L4_releasedSlot()
{
    //qDebug("pushButton_L4_released");

    //新调用方式
    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_L5_releasedSlot()
{
    //qDebug("pushButton_L5_released");

    //新调用方式
    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_L6_releasedSlot()
{
    //qDebug("pushButton_L6_released");

    //新调用方式
    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_L7_releasedSlot()
{
    //qDebug("pushButton_L6_released");

    //新调用方式
//    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_R1_pressedSlot()
{
    //qDebug("pushButton_R1_pressed");
    axisNumber = EM_AXIS_1;
    direction = EM_DIRECTION_RIGHT;
    emit setRightViewCurrentAxis(EM_AXIS_1);

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_R2_pressedSlot()
{
    //qDebug("pushButton_R2_pressed");
    axisNumber = EM_AXIS_2;
    direction = EM_DIRECTION_RIGHT;
    emit setRightViewCurrentAxis(EM_AXIS_2);

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_R3_pressedSlot()
{
    //qDebug("pushButton_R3_pressed");
    axisNumber = EM_AXIS_3;
    direction = EM_DIRECTION_RIGHT;
    emit setRightViewCurrentAxis(EM_AXIS_3);

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_R4_pressedSlot()
{
    //qDebug("pushButton_R4_pressed");
    axisNumber = EM_AXIS_4;
    direction = EM_DIRECTION_RIGHT;
    emit setRightViewCurrentAxis(EM_AXIS_4);

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_R5_pressedSlot()
{
    //qDebug("pushButton_R5_pressed");
    axisNumber = EM_AXIS_5;
    direction = EM_DIRECTION_RIGHT;
    emit setRightViewCurrentAxis(EM_AXIS_5);

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_R6_pressedSlot()
{
    //qDebug("pushButton_R6_pressed");
    axisNumber = EM_AXIS_6;
    direction = EM_DIRECTION_RIGHT;
    emit setRightViewCurrentAxis(EM_AXIS_6);

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_R7_pressedSlot()
{
//    //qDebug("pushButton_R6_pressed");
//    axisNumber = EM_AXIS_7;
//    direction = EM_DIRECTION_RIGHT;
//    emit setRightViewCurrentAxis(EM_AXIS_7);

//    //jogTimer->start(JOG_TIME);
//    //新调用方式
//    moveOperation->startJogMove(axisNumber,direction);
}

void JogDelegate::pushButton_R1_releasedSlot()
{
    //qDebug("pushButton_R1_released");

    //新调用方式
    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_R2_releasedSlot()
{
    //qDebug("pushButton_R2_released");

    //新调用方式
    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_R3_releasedSlot()
{
    //qDebug("pushButton_R3_released");

    //新调用方式
    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_R4_releasedSlot()
{
    //qDebug("pushButton_R4_released");

    //新调用方式
    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_R5_releasedSlot()
{
    //qDebug("pushButton_R5_released");

    //新调用方式
    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_R6_releasedSlot()
{
    //qDebug("pushButton_R6_released");

    //新调用方式
    moveOperation->stopJogMove();
}

void JogDelegate::pushButton_R7_releasedSlot()
{
    //qDebug("pushButton_R6_released");

    //新调用方式
//    moveOperation->stopJogMove();
}

void JogDelegate::updateDataAndView()
{
    updateAxisPropertyShow();

    updateAxisValueShow();

}

void JogDelegate::changeLanguageSlot(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/jogDelEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
}
